﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Resources;
using dao;
using MySql.Data.MySqlClient;
using common;
namespace GTVAccountingSystem.view.usercontrol
{
    public partial class uclGroupsDetailTab : UserControl
    {
        ResourceManager rm = Constant.RM;
        CultureInfo culture = Constant.CULTURE;
        UsersDAO usersDAO = new UsersDAO();
        UserGroupDAO groupDAO = new UserGroupDAO();
        PermissionDAO permissionDAO = new PermissionDAO();
        DataRow row;
        String idCurrentGroup = "na";
        DataTable dtGroupPermissionDetails = new DataTable();
        int pageIndex = 0;
        public uclGroupsDetailTab()
        {
            InitializeComponent();
        }
        public void getDataFromUserGroup(DataRow row)
        {
            if (!idCurrentGroup.Equals(row["IDGroup"].ToString()))
            {
                this.row = row;
                if (tabControlGroup.SelectedIndex == 0)
                {
                    showDetailsGroup(row["IDGroup"].ToString());
                }
                else if (tabControlGroup.SelectedIndex == 1)
                {
                    loadAllTabOfTabPermission();
                }
                idCurrentGroup = row["IDGroup"].ToString();
            }
        }
        public void showDetailsGroup(String idGroup)
        {
            row = groupDAO.getUserGroupByID(idGroup).Rows[0];
        }

        private void btnNewGroup_Click(object sender, EventArgs e)
        {

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {

        }
        public void loadTableGroupPermissionDetails()
        {
            dtGroupPermissionDetails = groupDAO.getAllUserGroupPermission(row["IDGroup"].ToString(), tabControlPermission.TabPages[tabControlPermission.SelectedIndex].Tag.ToString());
            dgvPermission.DataSource = dtGroupPermissionDetails;
            if (dgvPermission.Columns.Count > 0)
            {
                dgvPermission.Columns.Add((DataGridViewTextBoxColumn)createColumnForDataGridView(rm.GetString("lblFunctionName", culture), "Textbox", "FunctionName"));
                dgvPermission.Columns.Add((DataGridViewCheckBoxColumn)createColumnForDataGridView(rm.GetString("lblView", culture), "Checkbox", "Pread"));
                dgvPermission.Columns.Add((DataGridViewCheckBoxColumn)createColumnForDataGridView(rm.GetString("lblAdd", culture), "Checkbox", "PWrite"));
                dgvPermission.Columns.Add((DataGridViewCheckBoxColumn)createColumnForDataGridView(rm.GetString("lblUpdate", culture), "Checkbox", "PEdit"));
                dgvPermission.Columns.Add((DataGridViewCheckBoxColumn)createColumnForDataGridView(rm.GetString("lblDelete", culture), "Checkbox", "PDelete"));
            }
        }
        public Object createColumnForDataGridView(String headerName, String type, String propertyName)
        {
            if (type.Equals("Checkbox"))
            {
                DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
                col.HeaderText = headerName;
                DataGridViewCheckBoxCell cell = new DataGridViewCheckBoxCell();
                cell.TrueValue = "1";
                cell.FalseValue = "0";
                col.CellTemplate = cell;
                col.DataPropertyName = propertyName;
                return col;
            }
            else
            {
                DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
                col.HeaderText = headerName;
                DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
                col.DataPropertyName = propertyName;
                col.ReadOnly = true;
                return col;
            }
        }
        private void dgvPermission_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                DataRow tempRow = dtGroupPermissionDetails.Rows[e.RowIndex];
                DataColumn tempCol = dtGroupPermissionDetails.Columns[e.ColumnIndex];
                if (!tempCol.ColumnName.Equals("FunctionName"))
                {
                    String status = dgvPermission.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                    if (status.Equals("1"))
                        status = "0";
                    else
                        status = "1";
                    if (tempCol.ColumnName.Equals("PRead"))
                    {
                        dtGroupPermissionDetails.Rows[e.RowIndex][e.ColumnIndex] = status;
                        if (status.Equals("0"))
                        {
                            for (int i = 2; i <= 5; i++)
                            {
                                dtGroupPermissionDetails.Rows[e.RowIndex][i] = status;
                            }
                        }
                    }
                    else
                    {
                        dtGroupPermissionDetails.Rows[e.RowIndex][e.ColumnIndex] = status;
                        if (status.Equals("1"))
                        {
                            dtGroupPermissionDetails.Rows[e.RowIndex][1] = status;
                            if (tempRow[2].Equals("1") && tempRow[3].Equals("1") && tempRow[4].Equals("1"))
                            {
                                dtGroupPermissionDetails.Rows[e.RowIndex][5] = status;
                            }
                        }
                    }
                    String read = dtGroupPermissionDetails.Rows[e.RowIndex]["Pread"].ToString();
                    String write = dtGroupPermissionDetails.Rows[e.RowIndex]["PWrite"].ToString();
                    String edit = dtGroupPermissionDetails.Rows[e.RowIndex]["PEdit"].ToString();
                    String delete = dtGroupPermissionDetails.Rows[e.RowIndex]["PDelete"].ToString();
                    String id = dtGroupPermissionDetails.Rows[e.RowIndex]["ID"].ToString();
                    String cmdText = "UPDATE GroupPermission SET PRead = '" + read + "', PWrite = '" + write + "', PEdit = '" + edit + "', PDelete = '" + delete + "' WHERE ID = " + id;
                    groupDAO.updateUserGroupPermission(cmdText, row["GroupName"].ToString());
                }
            }
        }
        private void dgvPermission_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if(e.RowIndex > -1)
                dgvPermission.Rows[e.RowIndex].Selected = true;
        }
        private void dgvPermission_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Control control = (Control)sender;
                Point p = control.PointToScreen(new Point(e.X, e.Y));
                cmsPermission.Show(p);
            }
        }
        public void loadAllTabOfTabPermission()
        {
            if (tabControlPermission.TabPages.Count == 0)
            {
                DataTable dtPermissionType = permissionDAO.getAllPermissionType();
                for (int i = 0; i < dtPermissionType.Rows.Count; i++)
                {
                    TabPage tabPage = new TabPage();
                    tabPage.Tag = dtPermissionType.Rows[i]["ID"].ToString();
                    tabPage.Text = dtPermissionType.Rows[i]["TypeName"].ToString();
                    tabControlPermission.TabPages.Add(tabPage);
                }
            }
            tabControlPermission.TabPages[pageIndex].Controls.Clear();
            pageIndex = tabControlPermission.SelectedIndex;
            loadTableGroupPermissionDetails();
            tabControlPermission.SelectedTab.Controls.Add(dgvPermission);
            if (dgvPermission.Rows.Count > 0)
                dgvPermission.Rows[0].Selected = true;
        }

        private void tabControlGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControlGroup.SelectedIndex == 0)
                showDetailsGroup(row["IDGroup"].ToString());
            else
            {
                loadAllTabOfTabPermission();
            }
        }

        private void tabControlPermission_SelectedIndexChanged(object sender, EventArgs e)
        {
            tabControlPermission.TabPages[pageIndex].Controls.Clear();
            pageIndex = tabControlPermission.SelectedIndex;
            loadTableGroupPermissionDetails();
            tabControlPermission.SelectedTab.Controls.Add(dgvPermission);
            if (dgvPermission.Rows.Count > 0)
                dgvPermission.Rows[0].Selected = true;
        }

        private void allPermissionThisFunctionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPermissionByRow("1");
        }

        private void allPermissionViewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPermissionByColumn(1, "PRead", "1");
        }
        public void setPermissionByColumn(int index, String colName, String status)
        {
            String cmdText = "";
            for (int i = 0; i < dgvPermission.Rows.Count; i++)
            {
                dtGroupPermissionDetails.Rows[i][colName] = status;
                String read = dtGroupPermissionDetails.Rows[i]["Pread"].ToString();
                String write = dtGroupPermissionDetails.Rows[i]["PWrite"].ToString();
                String edit = dtGroupPermissionDetails.Rows[i]["PEdit"].ToString();
                String delete = dtGroupPermissionDetails.Rows[i]["PDelete"].ToString();
            }
            groupDAO.updateUserGroupPermissionbyColumn(colName,row["IDGroup"].ToString(), tabControlPermission.SelectedTab.Tag.ToString(), status, row["GroupName"].ToString());
        }
        public void setPermissionByRow(String status)
        {
            String cmdText = "";
            int row = 0;
            if (dgvPermission.SelectedCells.Count > 0)
            {
                row = dgvPermission.SelectedCells[0].RowIndex;
            }
            else if (dgvPermission.SelectedRows.Count > 0)
            {
                row = dgvPermission.Rows.IndexOf(dgvPermission.SelectedRows[0]);
            }
            for (int i = 1; i <= 4; i++)
            {
                dtGroupPermissionDetails.Rows[row][i] = status;
            }
            String id = dtGroupPermissionDetails.Rows[row]["ID"].ToString();
            cmdText = "UPDATE GroupPermission SET PRead = '" + status + "', PWrite = '" + status + "', PEdit = '" + status + "', PDelete = '" + status + "' WHERE ID = " + id;
            groupDAO.updateUserGroupPermission(cmdText, this.row["GroupName"].ToString());
        }
        public void setAllPermissionByStatus(String status)
        {
            String cmdText = "";
            for (int i = 0; i < dgvPermission.Rows.Count; i++)
            {
                dtGroupPermissionDetails.Rows[i]["PRead"] = status;
                dtGroupPermissionDetails.Rows[i]["PWrite"] = status;
                dtGroupPermissionDetails.Rows[i]["PEdit"] = status;
                dtGroupPermissionDetails.Rows[i]["PDelete"] = status;
            }
            groupDAO.updateUserGroupPermissionbyColumn("", row["IDGroup"].ToString(), tabControlPermission.SelectedTab.Tag.ToString(), status, row["GroupName"].ToString());
        }
        private void noPermissionViewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setAllPermissionByStatus("0");
        }

        private void allPermissionAddToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPermissionByColumn(2, "PWrite", "1");
        }

        private void noPermissionAddToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPermissionByColumn(2, "PWrite", "0");
        }

        private void allToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPermissionByColumn(3, "PEdit", "1");
        }

        private void noPermissionUpdateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPermissionByColumn(3, "PEdit", "0");
        }

        private void allPermissionDeleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPermissionByColumn(4, "PDelete", "1");
        }

        private void noPermissionDeleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPermissionByColumn(4, "PEdit", "0");
        }

        private void allPermissionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setAllPermissionByStatus("1");
        }

        private void thisFunctionHasNoPermissionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPermissionByRow("0");
        }
    }
}
